<?php

/**
 * @see: http://de.selfhtml.org/html/referenz/attribute.htm
 */
class tx_zlib_viewhelper_form extends tx_zlib_abstract_viewhelper
{

	/**
	 * @var tx_zlib_viewhelper
	 */
	private $helper;

	/**
	 * @var string
	 */
	private $id;

	/**
	 * @var string
	 */
	private $class;

    /**
     * @return tx_zlib_viewhelper_form
     */
	public function form()
	{
		$this->helper = $this->getHelper();
        return $this;
	}

    /**
     * @param string $id
     * @return tx_zlib_viewhelper_form
     */
	public function setId($id)
	{
		$this->id = $this->normalizeId($id);
		return $this;
	}

    /**
     * @param string $class
     * @return tx_zlib_viewhelper_form
     */
	public function setClass($class)
	{
		$this->class = $this->normalizeClass($class);
		return $this;
	}

    /**
     * @param string $name
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function text($name, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
		$this->addClass($attribs, 'text');

        $xhtml = '<input type="text"'
                . ' name="' . $this->helper->escape($name) . '"'
                . ' value="' . $this->helper->escape($value) . '"'
                . $this->htmlAttribs($attribs)
                . $this->getEndTag();

        return $xhtml;
	}

    /**
     * @param string $name
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function textarea($name, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'textarea');

        if (empty($attribs['rows'])) {
            $attribs['rows'] = 5;
        }
        if (empty($attribs['cols'])) {
            $attribs['cols'] = 40;
        }

        $xhtml = '<textarea name="' . $this->helper->escape($name) . '"'
                . $this->htmlAttribs($attribs) . '>'
                . $this->helper->escape($value)
                . '</textarea>';

        return $xhtml;
	}

    /**
     * @param string $name
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function submit($name, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'submit');

        $xhtml = '<input type="submit"'
               . ' name="' . $this->helper->escape($name) . '"'
               . ' value="' . $this->helper->escape($value) . '"'
               . $this->htmlAttribs($attribs)
               . $this->getEndTag();

        return $xhtml;
	}

    /**
     * @param string $name
     * @param string|array $value
     * @param array $options
     * @param array $attribs
     * @return string
     */
	public function select($name, $value = null, $options = array(), $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'select');

        $isMultiple = isset($attribs['multiple']) && ($attribs['multiple'] = 'multiple');
        if($isMultiple) {
            $value = (array)$value;
            $name .= '[]';
        }

        $xhtml = '<select name="' . $this->helper->escape($name) . '"'
                . $this->htmlAttribs($attribs)
                . '>'
                . $this->selectOptions($options, $value)
                . '</select>';

        return $xhtml;
	}

    /**
     * @param array $attribs
     * @param string|array $value
     * @return string
     */
	public function selectOptions(array $options, $value = null)
	{
		$xhtml = '';
		foreach($options AS $key=>$optValue) {
		    if(is_array($value)) {
    		    $selected = (in_array($key, $value) ? ' selected="selected"' : '');
		    } else {
    		    $selected = (($key == $value) ? ' selected="selected"' : '');
		    }
	        $xhtml .= '<option'
	               . ' value="' . $this->helper->escape($key) . '"'
	               . $selected
                   . '>'
                   . $this->helper->escape($optValue)
	               . '</option>';
		}
		return $xhtml;
	}

    /**
     * @param string $name
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function reset($name, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'reset');

        $xhtml = '<input type="reset"'
               . ' name="' . $this->helper->escape($name) . '"';

        if (! empty($value)) {
            $xhtml .= ' value="' . $this->helper->escape($value) . '"';
        }

        $xhtml .= $this->htmlAttribs($attribs)
        	   . $this->getEndTag();
        return $xhtml;
	}

    /**
     * @param string $name
     * @param string $value
     * @param array $options
     * @param array $attribs
     * @return string
     */
	public function radio($name, $value = null, $options = array(), $attribs = array())
	{
		$xhtml = '';

		if(!empty($options)) {

		    $groupAttribs = array();
		    $this->addClass($groupAttribs, 'radiogroup');
			$xhtml .= sprintf('<div%s>', $this->htmlAttribs($groupAttribs));

			foreach($options AS $key=>$option) {

			    // An index array has numeric (integer) keys automatically
			    if(is_numeric($key)) {
			        $key = strval($key);
			    }

				if(is_array($option)) {
					$optValue = $option['value'];
					$optLabel = $option['label'];
					$optAttribs = array_merge($attribs, $option);
				} else {
					$optValue = $key;
					$optLabel = $option;
					$optAttribs = $attribs;
				}

				$this->addId($optAttribs, $name, $optValue);
				$this->addClass($optAttribs, 'radio');

				unset($optAttribs['value']);
				unset($optAttribs['label']);

				$itemAttribs = array();
				$this->addClass($itemAttribs, 'radioitem');
                $xhtml .= sprintf('<div%s>', $this->htmlAttribs($itemAttribs));

		        $xhtml .= '<input type="radio"'
		               . ' name="' . $this->helper->escape($name) . '"'
		               . ' value="' . $this->helper->escape($optValue) . '"'
					   . $this->htmlAttribs($optAttribs)
		               . (($optValue === $value) ? ' checked="checked"' : '')
	                   . $this->getEndTag()
	                   . '<label'
		               . ' for="' . $this->helper->escape($optAttribs['id']) . '"'
	                   . '>'
	                   . $this->helper->escape($optLabel)
	                   . '</label>';

				$xhtml .= '</div>';

			}

			$xhtml .= '</div>';
		}

		return $xhtml;
	}

    /**
     * @param string $name
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function password($name, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'password');

        $xhtml = '<input type="password"'
                . ' name="' . $this->helper->escape($name) . '"'
                . ' value="' . $this->helper->escape($value) . '"'
                . $this->htmlAttribs($attribs)
                . $this->getEndTag();

        return $xhtml;
	}

    /**
     * @param string $name
     * @param string $checkedValue
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function checkbox($name, $checkedValue = null, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'checkbox');

        $xhtml = '<input type="checkbox"'
               . ' name="' . $this->helper->escape($name) . '"'
               . ' value="' . $this->helper->escape($value) . '"'
               . $this->htmlAttribs($attribs)
               . (($value == $checkedValue) ? ' checked="checked"' : '')
               . $this->getEndTag();

		return $xhtml;
	}

    /**
     * @param string $name
     * @param string $values
     * @param array $options
     * @param array $attribs
     * @return string
     */
	public function multiCheckbox($name, $values = array(), $options = array(), $attribs = array())
	{

		$xhtml = '';

		if(!empty($options)) {

    	    if(!is_array($values)) {
    	       $values = (array)$values;
    	    }

		    $groupAttribs = array();
		    $this->addClass($groupAttribs, 'checkboxgroup');
			$xhtml .= sprintf('<div%s>', $this->htmlAttribs($groupAttribs));

			foreach($options AS $key=>$option) {

				if(is_array($option)) {
					$optValue = $option['value'];
					$optLabel = $option['label'];
					$optAttribs = array_merge($attribs, $option);
				} else {
					$optValue = $key;
					$optLabel = $option;
					$optAttribs = $attribs;
				}

				$this->addId($optAttribs, $name, $optValue);
				$this->addClass($optAttribs, 'ckeckbox');

				unset($optAttribs['value']);
				unset($optAttribs['label']);

				$itemAttribs = array();
				$this->addClass($itemAttribs, 'checkboxitem');
                $xhtml .= sprintf('<div%s>', $this->htmlAttribs($itemAttribs));

		        $xhtml .= '<input type="checkbox"'
		               . ' name="' . $this->helper->escape($name) . '[]"'
		               . ' value="' . $this->helper->escape($optValue) . '"'
					   . $this->htmlAttribs($optAttribs)
		               . (in_array($optValue, $values) ? ' checked="checked"' : '')
	                   . $this->getEndTag()
	                   . '<label'
		               . ' for="' . $this->helper->escape($optAttribs['id']) . '"'
	                   . '>'
	                   . $this->helper->escape($optLabel)
	                   . '</label>';

				$xhtml .= '</div>';

			}

			$xhtml .= '</div>';
		}

		return $xhtml;
	}

    /**
     * @param string $name
     * @param array $attribs
     * @return string
     */
	public function file($name, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'file');

        $xhtml = '<input type="file"'
                . ' name="' . $this->helper->escape($name) . '"'
                . $this->htmlAttribs($attribs)
                . $this->getEndTag();

        return $xhtml;
	}

    /**
     * @param string $name
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function button($name, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'button');

        $xhtml = '<input type="button"'
               . ' name="' . $this->helper->escape($name) . '"'
               . ' value="' . $this->helper->escape($value) . '"'
               . $this->htmlAttribs($attribs)
               . $this->getEndTag();

        return $xhtml;
	}

    /**
     * @param string $name
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function hidden($name, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'hidden');

        $xhtml = '<input type="hidden"'
             . ' name="' . $this->helper->escape($name) . '"'
             . ' value="' . $this->helper->escape($value) . '"'
             . $this->htmlAttribs($attribs)
             . $this->getEndTag();

		return $xhtml;
	}

    /**
     * @param string $name
     * @param string $src
     * @param string $value
     * @param array $attribs
     * @return string
     */
	public function image($name, $src, $value = null, $attribs = array())
	{
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'image');

        if (isset($value) && !empty($value)) {
            $value = ' value="' . $this->helper->escape($value) . '"';
        } else {
            $value = '';
        }

        $xhtml = '<input type="image"'
                . ' name="' . $this->helper->escape($name) . '"'
                . ' src="' . $this->helper->escape($src) . '"'
                . $value
                . $this->htmlAttribs($attribs)
                . $this->getEndTag();

        return $xhtml;
	}

    /**
     *
     * @return string
     */
	private function getEndTag()
	{
		return " />";
	}

    /**
     * @param string $action
     * @param string $method
     * @param array $attribs
     * @return string
     */
	public function start($action, $method='get', $attribs = array())
	{
		//$this->addId($attribs);
		if(empty($attribs['class'])) {
		    $class = 'form';
            if(!empty($this->class)) {
                $class .= ' ' . $this->class;
            }
			$attribs['class'] = $class;
		}

        $xhtml = '<form'
                . ' action="' . $this->helper->escape($action) . '"'
                . ' method="' . $this->helper->escape($method) . '"'
                . $this->htmlAttribs($attribs)
                . '>';

        return $xhtml;
	}

    /**
     * @return string
     */
	public function end()
	{
		return '</form>';
	}

    /**
     * @param array $attribs
     * @param string $name
     * @param string $value
     * @return void
     */
	private function addId(array &$attribs, $name, $value=null)
	{
		if(empty($attribs['id'])) {
            $parts = array();
			if(!empty($this->id)) {
                $parts[] = $this->id;
            }
            $parts[] = $this->normalizeId($name);
			if(!empty($value)) {
                $parts[] = $this->normalizeId($value);
            }
			$attribs['id'] = implode($this->idDelim, $parts);
		}
	}

	/**
	 * @param array $attribs
	 * @param string $type
	 * @return void
	 */
	private function addClass(array &$attribs, $type)
	{
		if(empty($attribs['class'])) {
		    $type = $this->normalizeClass($type);
		    $class = 'form-' . $type;
            if(!empty($this->class)) {
                $class .= ' ' . $this->class . $this->classDelim . $type;
            }
			$attribs['class'] = $class;
		}
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_form.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_form.php']);
}
